
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flutter/animation.dart';
import 'package:klondike/klondike/klondike_game.dart';
import 'package:klondike/klondike/klondike_world.dart';

class EffectUtils {

  static Future<void> doMove(
    List<PositionComponent> positionComponents,
    List<Vector2> to, {
    double speed = 10.0,
    double start = 0.0,
    Curve curve = Curves.easeOutQuad,
  }) async {
    assert(speed > 0.0, 'Speed must be > 0 widths per second');

    var effects = [];
    for(int i=0; i<positionComponents.length; i++) {
      final dt = (to[i] - positionComponents[i].position).length / (speed * KlondikeWorld.cardWidth);
      assert(dt > 0.0, 'Distance to move must be > 0');
      positionComponents[i].priority = 100 + i;

      var effect = MoveToEffect(
          to[i],
          EffectController(duration: dt, startDelay: start, curve: curve),
        );
      positionComponents[i].add(effect);
      effects.add(effect);
    }
    
    for (var effect in effects) {
      await effect.completed;
    }
  }

  static Future<void> shake(List<PositionComponent> positionComponents) async {

    var effects = [];

    for(int i=0; i<positionComponents.length; i++) {
          var effect = SequenceEffect([
            MoveByEffect(
              Vector2( 0.07 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.05, alternate: true),
            ),
            MoveByEffect(
              Vector2( -0.07 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.05, alternate: true),
            ),
            MoveByEffect(
              Vector2( 0.05 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.03, alternate: true),
            ),
            MoveByEffect(
              Vector2( -0.05 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.03, alternate: true),
            ),
            MoveByEffect(
              Vector2( 0.03 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.01, alternate: true),
            ),
            MoveByEffect(
              Vector2( -0.03 * KlondikeGame.cardWidth, 0),
              EffectController(duration: 0.01, alternate: true),
            ),
          ]);
      positionComponents[i].add(effect);
      effects.add(effect);
    }

    for (var effect in effects) {
      await effect.completed;
    }
  }
}